package utils;

import utils.file.FileUtils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * Created by Administrator on 2015/11/24.
 */
@SuppressWarnings("deprecation")
//@Deprecated
public class FileRecorder {

    String desFileName;


    public FileRecorder(String desFileName) {
        FileUtils.FilePathResolver resolver = new FileUtils.FilePathResolver(desFileName);
        FileUtils.createDir(resolver.getFilePath());
        this.desFileName = desFileName;
    }


    /**
     * 记录一条字符串到指定文件末尾处
     *
     * @param src         待记录字符串
     */
    @Deprecated
    public void recordInFile(String src) {
        recordInFile(new String[]{src}, true);
    }


    /**
     * 记录一条字符串到指定文件末尾处
     *
     * @param src         待记录字符串
     */
    @Deprecated
    public void recordInFile(String src, boolean isAppend) {
        recordInFile(new String[]{src}, isAppend);
    }


    /**
     * 记录一个字符串数组到指定文件末尾处
     *
     * @param src         待记录字符串
     */
    @Deprecated
    public void recordInFile(String[] src) {
        recordInFile(src, true);
    }


    /**
     * 记录一个字符串数组到指定文件末尾处
     *
     * @param src         待记录字符串
     */
    @Deprecated
    public void recordInFile(String[] src, boolean isAppend) {
        File file = new File(desFileName);
        if (file.isDirectory()) {
            return;
        }
        BufferedWriter bw = null;
        try {
            if ((!file.exists()) && (!file.createNewFile())) {
                System.out.println("Failed to create new File" + desFileName);
                return;
            }
            bw = new BufferedWriter(new FileWriter(file, isAppend));
            for (String s : src) {
                bw.write(s + "\r\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bw != null) {
                try {
                    bw.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }


    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 10; i++) {
            new FileRecorder("test.txt").recordInFile(i + "");
            Thread.sleep(2000);
        }
    }
}